package com.wd.live.view.adpater;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.wd.live.R;
import com.wd.live.view.CardAdapter;
import com.wd.live.view.CardItem;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>项目名称:LookFor2_0</p>
 * <p>包名:com.wd.live.view.adapter</p>
 * <p>简述:卡片适配器</p>
 *
 * @author 孟令高
 * @date 2022/11/11
 */
public class CardPagerAdapter extends PagerAdapter implements CardAdapter {
    private List<CardView> viewList;
    private List<CardItem> cardItemList;
    private float aFloat;

    public CardPagerAdapter() {
        cardItemList = new ArrayList<>();
        viewList = new ArrayList<>();
    }

    public void addCardItem(CardItem item) {
        viewList.add(null);
        cardItemList.add(item);
    }

    public float getBaseElevation() {
        return aFloat;
    }

    @Override
    public CardView getCardViewAt(int position) {
        return viewList.get(position);
    }

    @Override
    public int getCount() {
        return cardItemList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.list_adapter, container, false);
        container.addView(view);
        bind(cardItemList.get(position), view);
        CardView cardView = (CardView) view.findViewById(R.id.cardView);

        if (aFloat == 0) {
            aFloat = cardView.getCardElevation();
        }

        cardView.setMaxCardElevation(aFloat * MAX_ELEVATION_FACTOR);
        viewList.set(position, cardView);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        viewList.set(position, null);
    }

    //按照卡片布局的Itme进行设置相应的数据
    private void bind(CardItem item, View view) {
        TextView textView = (TextView) view.findViewById(R.id.list_view);
        TextView contentTextView = (TextView) view.findViewById(R.id.contentTextView);
        ImageView byId = (ImageView) view.findViewById(R.id.head_portraits);
        TextView theNumberOfFans = (TextView) view.findViewById(R.id.the_number_of_fans);
        ImageView table = (ImageView) view.findViewById(R.id.page_table);
        ImageView xingbie1 = (ImageView) view.findViewById(R.id.sex);
        TextView live_title = (TextView) view.findViewById(R.id.live_title);
        TextView livetimes = (TextView) view.findViewById(R.id.live_time);
        TextView live_video_duration = (TextView) view.findViewById(R.id.live_video_duration);
        ImageView paiss = (ImageView)view.findViewById(R.id.pai);
        TextView pai1=(TextView)view.findViewById(R.id.pai1);
        textView.setText(item.getNickName());
        Glide.with(view).load(item.getPersonalImage()).circleCrop().into(byId);
        theNumberOfFans.setText("粉丝数："+item.getFanSum());
        Glide.with(view).load(item.getPersonalImage()).apply(RequestOptions.bitmapTransform(new RoundedCorners(25))).into(table);
        if(item.getRank()==1){
            paiss.setImageResource(R.mipmap.medal1);
        }else if (item.getRank()==2){
            paiss.setImageResource(R.mipmap.medal2);
        }else if(item.getRank()==3){
            paiss.setImageResource(R.mipmap.medal3);
        }else {
            paiss.setVisibility(View.GONE);
            pai1.setVisibility(View.VISIBLE);
            pai1.setText(item.getRank()+"");
        }
        if (item.getSex()==1){
            xingbie1.setImageResource(R.mipmap.man);
        }else if (item.getSex()==2){
            xingbie1.setImageResource(R.mipmap.gerry);
        }

        if (item.getLiveStatus()==2){
            live_title.setText(item.getLiveVideoDuration());
            livetimes.setText(item.getNickName());
            String liveVideoDuration = item.getLiveVideoDuration();
            live_video_duration.setText(liveVideoDuration);
        }
    }


}
